﻿using BlogEngine.Gallery.Data;
using BlogEngine.Gallery.Models;
using System.IO;
using System.Web.Mvc;

namespace BlogEngine.Gallery.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            string returnCode = (string)this.RouteData.Values["id"];
            ViewBag.Message = "";

            if (returnCode == "0")
            {
                ViewBag.Success = true;
                ViewBag.Message = "Thank you for contributing to BlogEngine.NET gallery. We will test and publish your package shortly.";
            }
            else
            {
                ViewBag.Success = false;
                if (returnCode == "1") ViewBag.Message = "Package file (.nupkg) is required";
                if (returnCode == "2") ViewBag.Message = "Package file must be NuGet file (.nupkg)";
                if (returnCode == "3") ViewBag.Message = "Icon file must be a valid image file (png, gif or jpg)";
            }

            ViewBag.Uploads = Data.Repository.GetUploaded();
            ViewBag.Rejected = Data.Repository.GetRejected();

            return View();
        }

        [HttpPost]
        public ActionResult Upload(string txtEmail)
        {
            #region Package
            var pkg = Request.Files["pkgFile"];

            if (pkg == null || pkg.ContentLength == 0)
                return RedirectToAction("Index", new { id = "1" });

            if (!pkg.FileName.ToLower().EndsWith(".nupkg"))
                return RedirectToAction("Index", new { id = "2" });

            var uploadsDir = Server.MapPath("~/Packages/Uploads/" + pkg.FileName.Replace(".nupkg", ""));

            if (!Directory.Exists(uploadsDir))
                Directory.CreateDirectory(uploadsDir);

            var pkgPath = Path.Combine(uploadsDir, pkg.FileName);

            if (System.IO.File.Exists(pkgPath))
                System.IO.File.Delete(pkgPath);

            pkg.SaveAs(pkgPath);
            #endregion

            #region Icon
            // save image if exists
            var img = Request.Files["imgFile"];
            if (img != null && img.ContentLength > 0)
            {
                var imgPath = Path.Combine(uploadsDir, img.FileName);

                var ip = imgPath.ToLower();
                if (ip.EndsWith(".png") || ip.EndsWith(".gif") || ip.EndsWith(".jpg"))
                {
                    if (System.IO.File.Exists(imgPath))
                        System.IO.File.Delete(imgPath);

                    img.SaveAs(imgPath);
                }
                else
                {
                    return RedirectToAction("Index", new { id = "3" });
                }
            }
            #endregion

            #region Email
            // save email if entered
            if (!string.IsNullOrEmpty(txtEmail))
            {
                var emailFile = Path.Combine(uploadsDir, pkg.FileName.Replace(".nupkg", ".txt"));

                if (System.IO.File.Exists(emailFile))
                    System.IO.File.Delete(emailFile);

                using (var writer = new System.IO.StreamWriter(emailFile, true))
                {
                    writer.WriteLine(txtEmail);
                }
            }
            #endregion

            #region Extra
            var pkgX = new PackageExtra();

            pkgX.Id = pkg.FileName.Replace(".nupkg", "");
            pkgX.DownloadCount = 0;
            pkgX.PkgType = PackageType.Extension;

            Repository.SaveUploaded(pkgX);
            #endregion

            return RedirectToAction("Index", new { id = "0" });
        }
    }
}